package com.test01_程序基础;

import org.junit.Test;

/**
 * 运算符
 */
public class YootkDemo_03 {

    /**
     * 数学运算符
     */
    @Test
    public void test1() {
        int a = 20;
        int b = 3;
        System.out.printf("%d + %d = %d%n", a, b, (a + b));
        System.out.printf("%d - %d = %d%n", a, b, (a - b));
        System.out.printf("%d * %d = %d%n", a, b, (a * b));
        System.out.printf("%d / %d = %d%n", a, b, (a / b));
        System.out.printf("%d %% %d = %d%n", a, b, (a % b));
    }

    /**
     * 自增自减运算符
     */
    @Test
    public void test2() {
        int a = 10;
        System.out.printf("%d++ = %d%n", a, (a++));
        int b = 10;
        System.out.printf("%d-- = %d%n", b, (b--));
        int c = 10;
        System.out.printf("++%d = %d%n", c, (++c));
        int d = 10;
        System.out.printf("--%d = %d%n", d, (--d));
    }

    /**
     * 关系运算符
     */
    @Test
    public void test3() {
        int a = 20;
        int b = 10;
        System.out.printf("%d > %d : %b%n", a, b, (a > b));
        System.out.printf("%d < %d : %b%n", a, b, (a < b));
        System.out.printf("%d == %d : %b%n", a, b, (a == b));
        System.out.printf("%d != %d : %b%n", a, b, (a != b));
    }

    /**
     * 三目运算符
     */
    @Test
    public void test4() {
        int a = 10;
        int b = 15;
        int max = a > b ? a : b;
        System.out.println(max);
    }

    /**
     * 逻辑运算符
     */
    @Test
    public void test5() {
        boolean a = true;
        boolean b = false;
        System.out.printf("%b && %b = %b%n", a, b, (a && b));
        System.out.printf("%b || %b = %b%n", a, b, (a || b));
        System.out.printf("!%b = %b%n", a, (!a));
    }

    /**
     * 位运算符
     */
    @Test
    public void test6() {
        int a = 13;     // 1101
        int b = 7;      // 0111
        System.out.printf("%d & %d = %d%n", a, b, (a & b));     // 按位与
        System.out.printf("%d | %d = %d%n", a, b, (a | b));     // 按位与
        System.out.printf("%d ^ %d = %d%n", a, b, (a ^ b));     // 按位异或
        System.out.printf("~%d = %d%n", a, (~a));               // 取反
    }

    /**
     * 移位运算符
     */
    @Test
    public void test7() {
        int a = 10;     // 1010
        int b = 2;
        System.out.printf("%d << %d = %d%n", a, b, (a << b));   // 左移运算符
        System.out.printf("%d >> %d = %d%n", a, b, (a >> b));   // 右移运算符
        System.out.printf("%d >>> %d = %d%n", a, b, (a >>> b));     // 无符号右移运算符
    }
}
